package com.wonders.ssm.controller;

import com.wonders.ssm.util.ImageCode;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 验证码请求
 * Created by lv on 2018/8/6.
 */
@RestController
public class ImageCodeController {

    @Resource(name = "imageCode")
    ImageCode imageCode;

    @RequestMapping("/loadCodeImage")
    public void getCodeImage(HttpServletRequest request, HttpServletResponse response){
        try {
            // 禁止图像缓存。
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setContentType("image/png");
            ServletOutputStream sos = response.getOutputStream();
            ImageIO.write(imageCode.getImage(request),"png",sos);
            //response.getOutputStream().flush();
            sos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
